//
//  OrderListFootView.swift
//  WineComing
//
//  Created by xiaozhi on 15/7/20.
//  Copyright (c) 2015年 xiaozhi. All rights reserved.
//

import UIKit
class OrderListFootView: UIView {
    var info1:UILabel!
    
    var line:UIView!
    var section:NSInteger! = 0
    var btnEventHandle:((NSString?,NSInteger)->Void)?
    var status:OrderStatus!
    @IBAction func buttonClick(sender:UIButton)->Void{
        btnEventHandle?(sender.titleLabel?.text,section)
    }
    func setTheStatus(status:OrderStatus){
        self.status = status
        switch status{
        case OrderStatus.NS_Status_Pending:
            break
        case OrderStatus.NS_Status_compelete:
            break
        case OrderStatus.NS_Status_Paid:
            break
        default:break
        }
    }
    func setupView(topHidden:Bool,buttonWithTitle titles:String...){
        let count = titles.count
        if titles[0] == ""{
            line.hidden = true
            var rect = info1.frame
            rect.size.height = bounds.size.height
            info1.frame = rect
            return
        }
        let offsetX:CGFloat = 0.0 //最右边button跟父视图的边距
        let distance:CGFloat = 15.0 //button间距
        let width:CGFloat = 80.0
        let height:CGFloat = 26.0
        let originy:CGFloat = bounds.size.height/2.0+7
        var button:UIButton!
        
        for i in 0..<count{
            let originx = bounds.size.width - CGFloat(count-i)*(distance+width)-offsetX
            button = UIButton(frame: CGRectMake(originx, originy, width, height))
            let onetitle = titles[i]
            button.titleLabel?.font = UIFont.systemFontOfSize(14)
            switch onetitle{
                case "删除":
                    button.layer.borderColor = UIColor.darkGrayColor().CGColor
                    button.layer.borderWidth = onePix
                    button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
                    button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Highlighted)
                case "评价":
                    button.layer.borderColor = commonRedColor.CGColor
                    button.layer.borderWidth = onePix
                    button.setTitleColor(commonRedColor, forState: UIControlState.Normal)
                    button.setTitleColor(commonRedColor, forState: UIControlState.Highlighted)
                case "继续支付":
                    fallthrough
                case "查看物流":
                    button.backgroundColor = commonRedColor
                    button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
                    button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Highlighted)
                default:break
            }
            button.layer.cornerRadius = 5.0
            button.setTitle(onetitle, forState: UIControlState.Normal)
            button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
            self.addSubview(button)
        }

    }
    func setInfo(info:String?,highLightInfo:String?){
        if let containInfo = info{
            if let highLightStr = highLightInfo{
                let desStr = containInfo as NSString
                let range:NSRange = desStr.rangeOfString(highLightStr)
                let attributeStr = NSMutableAttributedString(string: containInfo)
                
                attributeStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkGrayColor(), range: NSMakeRange(0, desStr.length))
                attributeStr.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(13), range: NSMakeRange(0, desStr.length))
                attributeStr.addAttribute(NSForegroundColorAttributeName, value:commonRedColor, range:range)
                attributeStr.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(16), range:range)
                info1.attributedText = attributeStr
            }
        }
    }
    func createSubViews()->Void{
        info1 = UILabel(frame: CGRectMake(0, 0, bounds.size.width-10, bounds.size.height/2.0))
        info1.textAlignment = NSTextAlignment.Right
        self.addSubview(info1)
        line = UIView(frame: CGRectMake(0, bounds.size.height/2.0+1, bounds.size.width, onePix))
        line.backgroundColor = commonBackgoundColor
        self.addSubview(line)
    }
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
    override init(frame: CGRect) {
        super.init(frame: frame)
        //        self.backgroundColor = UIColor(patternImage: UIImage(named: "tab-bg")!)
        backgroundColor = UIColor.whiteColor()
        self.clipsToBounds = true
        self.createSubViews()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.createSubViews()
    }
}
